package com.lzx.adx.report.enums;

import com.lzx.adx.report.dto.ReportDTO;
import org.apache.commons.lang3.StringUtils;
import java.util.function.Function;

public enum DeviceType {
    IMEI("imei", ReportDTO::getImei),
    IMEI_MD5("imeiMd5", ReportDTO::getImeiSum),
    OAID("oaid", ReportDTO::getOaid),
    OAID_MD5("oaidMd5", ReportDTO::getOaidSum);

    private final String type;
    private final Function<ReportDTO, String> valueExtractor;

    DeviceType(String type, Function<ReportDTO, String> valueExtractor) {
        this.type = type;
        this.valueExtractor = valueExtractor;
    }

    public String getType() {
        return type;
    }

    public String extractValue(ReportDTO reportDTO) {
        return valueExtractor.apply(reportDTO);
    }

    public static DeviceType fromReportDTO(ReportDTO reportDTO) {
        if (StringUtils.isNotBlank(reportDTO.getImei()) && !StringUtils.contains(reportDTO.getImei(), "__")) {
            return IMEI;
        } else if (StringUtils.isNotBlank(reportDTO.getImeiSum()) && !StringUtils.contains(reportDTO.getImeiSum(), "__")) {
            return IMEI_MD5;
        } else if (StringUtils.isNotBlank(reportDTO.getOaid()) && !StringUtils.contains(reportDTO.getOaid(), "__")) {
            return OAID;
        } else if (StringUtils.isNotBlank(reportDTO.getOaidSum()) && !StringUtils.contains(reportDTO.getOaidSum(), "__")) {
            return OAID_MD5;
        }
        return null;
    }
}